<template>
  <div>
    <div class="crumbs">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item><i class="el-icon-date"></i> 轮播管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <el-button type="primary" style="margin-bottom: 20px" @click="dialogFormVisible = true,formData.title = '',formData.imageUrl = ''">新增</el-button>
    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        label="标题"
        width="180">
        <template scope="scope">
          <span style="margin-left: 10px">{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="图片"
        width="180">
        <template scope="scope">
           <img :src="baseImgPath + scope.row.imageUrl" class="avatar">
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button
            size="small"
            @click="dialogFormVisible = true">编辑</el-button>
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.row.id,scope.row.imageUrl,scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="图片管理" :visible.sync="dialogFormVisible">
      <el-form :model="formData" :rules="rules" ref="formData">
        <el-form-item label="标题"  prop="title">
          <el-input v-model="formData.title" auto-complete="off"></el-input>
        </el-form-item>
        <el-upload
          class="avatar-uploader"
          action="api/uploadimg"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="formData.goodsImg" :src="baseImgPath+formData.imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <el-button type="primary" @click="submitForm('formData')">提交</el-button>
        <el-button>重置</el-button>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import VueCoreImageUpload from "vue-core-image-upload";
import axios from "axios";
import { baseImgPath } from "@/config/env";

export default {
  data: function() {
    return {
      baseImgPath,
      tableData: [],
      dialogFormVisible: false,
      formData: {
        title: "",
        imageUrl: ""
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        imageUrl: [{ required: true, message: "请上传图片", trigger: "blur" }]
      }
    };
  },
  components: {
    VueCoreImageUpload
  },
  mounted() {
    this.initData();
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    handleAvatarSuccess(res, file) {
      if (res.status == 1) {
        this.formData.imageUrl = res.image_path;
      } else {
        this.$message.error("上传图片失败！");
      }
    },
    initData() {
      axios.post("ad/search").then(res => {
        let result = res.data;
        result.forEach((item, index) => {
          const formList = {
            title: item.title,
            imageUrl: item.imgFileName,
            id: item.id
          };
          this.tableData.push(formList);
        });
      });
    },
    submitForm(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          var params = new URLSearchParams();
          params.append("title", this.formData.title);
          params.append("imgFileName", this.formData.imageUrl);
          axios.post("ad/add", params).then(res => {
            console.log(res)
            let result = res.data;
            if (result.status == 1) {
              this.$message({
                type: "success",
                message: "添加成功"
              });
              this.initData();
              this.formData.title = result.title;
              this.formData.imageUrl = result.imageUrl;
              this.dialogFormVisible = false;
            } else {
              this.$message({
                type: "fail",
                message: "添加失败"
              });
            }
          });
        } else {
          this.$notify.error({
            title: "错误",
            message: "请输入正确的信息",
            offset: 100
          });
          return false;
        }
      });
    },
    handleDelete(id, imgFileName, index) {
      var params = new URLSearchParams();
      params.append("id", id);
      params.append("imgFileName", imgFileName);
      axios.post("ad/delete", params).then(res => {
        let result = res.data;
        if (result.status == 1) {
          this.$message({
            type: "success",
            message: "删除成功"
          });
          this.tableData.splice(index, 1);
        } else {
          this.$message({
            type: "fail",
            message: "删除失败"
          });
        }
      });
    }
  }
};
</script>

<style scoped>
.content-title {
  font-weight: 400;
  line-height: 50px;
  margin: 10px 0;
  font-size: 22px;
  color: #1f2f3d;
}
.pre-img {
  width: 150px;
  height: 150px;
  margin-bottom: 20px;
  vertical-align: middle;
}

.js-btn-crop {
  display: inline-block;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
